package org.dromara.system.controller.system;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.system.domain.ReceiveDealRecordsReqDTO;
import org.dromara.system.enums.NotificationEnum;
import org.dromara.system.factory.PlatformTypeFactory;
import org.dromara.system.service.TradingService;
import org.dromara.system.service.impl.trading.AbstractTradingService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/trading")
public class TradingController {

    private final TradingService tradingService;

    @PostMapping("/kytTrading")
    @SaIgnore
    public R<Boolean> KYTTrading( @RequestBody ReceiveDealRecordsReqDTO receiveDealRecords) {

        return R.ok(tradingService.kytTrading(receiveDealRecords));
    }

    @SaIgnore
    @PostMapping(value = {"{path}/{type}/{majorKey}", "{path}/{type}"})
    public Object tradingWater(@PathVariable("path") String path,
                               @PathVariable("type") String type,
                               @PathVariable(value = "majorKey", required = false) Integer majorKey,
                               @RequestBody String mapBody) {
        NotificationEnum notificationEnum = NotificationEnum.findOneByCode(path);
        //抛出异常 没有将异常重置 导致重试方法阻塞
        AbstractTradingService abstractTradingService = PlatformTypeFactory.get(notificationEnum);
        if (ObjectUtil.isNull(abstractTradingService)) {
            return R.fail("渠道不存在");
        }
        return abstractTradingService.tradingWaterBody(type, majorKey, mapBody);
    }


}
